ShaguScore = CreateFrame( "Frame" , "ShaguScoreTooltip", GameTooltip )


function rgbToHex(r, g, b)  
    -- 确保输入值在0到255之间  
    r = math.max(0, math.min(r, 255))  
    g = math.max(0, math.min(g, 255))  
    b = math.max(0, math.min(b, 255))  
  
    -- 将RGB值转换为十六进制字符串  
    r_hex = string.format("%02X", r)  
    g_hex = string.format("%02X", g)  
    b_hex = string.format("%02X", b)  
  
    -- 拼接结果  
    return r_hex .. g_hex .. b_hex  
end  


local function printLine(message)
  if message then
    -- message:SetTextColor(r,g,b,1)
    DEFAULT_CHAT_FRAME:AddMessage("|cffff8000EmberSocre:|r " .. message);
  end
end


ShaguScore:SetScript("OnShow", function()
  if GameTooltip.itemLink then
    local _, _, itemID = string.find(GameTooltip.itemLink, "item:(%d+):%d+:%d+:%d+")
    local _, _, itemLink = string.find(GameTooltip.itemLink, "(item:%d+:%d+:%d+:%d+)");

    if not itemLink then return end

    local itemName, _, itemRarity, _, _, _, _, itemSlot, _ = GetItemInfo(itemLink)


    local itemLevel = GetItemLevel(itemID) or 0
    local r,g,b = GetItemQualityColor(itemRarity)

    local score = ShaguScore:Calculate(itemSlot, itemRarity, itemLevel)
    if score and score > 0 then
      GameTooltip:AddLine("EmberScore: " .. score, r, g, b)

      GameTooltip:Show()
    end
  end
end)

ShaguScore:SetScript("OnHide", function()
  GameTooltip.itemLink = nil
end)

-- target inspect
ShaguScoreHookInspectUnit = InspectUnit
function InspectUnit(unit)
  ShaguScoreHookInspectUnit(unit)
  ShaguScore.Inspect = ShaguScore.Inspect or CreateFrame("Frame", nil, InspectModelFrame)
  ShaguScore.Inspect:SetFrameStrata("HIGH")
  ShaguScore.Inspect:SetWidth(200)
  ShaguScore.Inspect:SetHeight(25)
  ShaguScore.Inspect:SetPoint("BOTTOM", 0, 0)
  ShaguScore.Inspect.text = ShaguScore.Inspect.text or ShaguScore.Inspect:CreateFontString("Status", "TOOLTIP", "GameFontNormal")
  ShaguScore.Inspect.text:SetPoint("CENTER", 0, 0)

  local score, r, g, b , count = ShaguScore:ScanUnit("target")
  if score and r and g and b then
    -- ShaguScore.Inspect.text:SetText("EmberScore: " .. score .. "  count:" .. count)
    ShaguScore.Inspect.text:SetText("EmberScore: " .. score)
    ShaguScore.Inspect.text:SetTextColor(r, g, b)
  end
end

-- player inspect
ShaguScore.CharacterFrame = CreateFrame("Frame", nil, CharacterModelFrame)
ShaguScore.CharacterFrame:SetFrameStrata("HIGH")
ShaguScore.CharacterFrame:SetWidth(200)
ShaguScore.CharacterFrame:SetHeight(50)
ShaguScore.CharacterFrame:SetPoint("BOTTOM", 0, 20)
ShaguScore.CharacterFrame.text = ShaguScore.CharacterFrame:CreateFontString("Status", "LOW", "GameFontNormal")
ShaguScore.CharacterFrame.text:SetPoint("CENTER", 0, 0)

--ShaguScore.CharacterFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
ShaguScore.CharacterFrame:RegisterEvent("UNIT_NAME_UPDATE")
ShaguScore.CharacterFrame:RegisterEvent("UNIT_PORTRAIT_UPDATE")
ShaguScore.CharacterFrame:RegisterEvent("BAG_UPDATE")
ShaguScore.CharacterFrame:SetScript("OnEvent", function()
  score, r, g, b ,count= ShaguScore:ScanUnit("player")
  if score and r and g and b then
    ShaguScore.CharacterFrame.text:SetText("EmberScore: " .. score)
    ShaguScore.CharacterFrame.text:SetTextColor(r, g, b)
  end
end)

-- functions
function ShaguScore:round(input, places)
  if not places then places = 0 end
  if type(input) == "number" and type(places) == "number" then
    local pow = 1
    for i = 1, places do pow = pow * 10 end
    return floor(input * pow + 0.5) / pow
  end
end

function ShaguScore:Calculate(slot, rarity, ilvl)
  if not rarity then return nil end
  return ilvl

end

function safeGetItemLevel(itemID)
    local success, level = pcall(GetItemLevel, itemId)
    if not success then
        return nil  -- 出错时返回nil
    end
    return level  -- 成功时返回物品等级
end

function ShaguScore:ScanUnit(target)
  if not UnitIsPlayer(target) then return nil end

  local count, ar, ag, ab, score = 0, 0, 0, 0, 0

  for i=1,19 do
    if GetInventoryItemLink(target, i) then
      local a = GetInventoryItemLink(target, i); a = string.sub(a,1+string.find(a,":"),string.len(a));
      local itemID = string.sub(a,1,string.find(a,":")-1);
      local _, _, itemLink = string.find(GetInventoryItemLink(target, i), "(item:%d+:%d+:%d+:%d+)");
      local itemName, _, itemRarity, _, _, _, _, itemSlot, _ = GetItemInfo(itemLink)
      if not itemName or not itemRarity or not itemSlot then return nil end
      local itemLevel = GetItemLevel(itemID) or 0
      --message(itemLevel)
      local r, g, b = .2, .2, .2
      local cscore = 0
      if itemSlot and itemSlot ~= "INVTYPE_TABARD" and itemSlot ~= "INVTYPE_SHIRT" and itemSlot ~= "INVTYPE_BODY" and itemLevel > 2 then
        r,g,b, _ = GetItemQualityColor(itemRarity)
        ar = ar + r ; ag = ag + g ; ab = ab + b
        cscore = ShaguScore:Calculate(itemSlot, itemRarity, itemLevel)
        if cscore >0 then
          score = score + cscore
          count = count + 1
        end
      end
    end
  end

  
  score = ShaguScore:round(score/(count),1);

  local ar = ShaguScore:round(ar / count, 2);
  local ag = ShaguScore:round(ag / count, 2);
  local ab = ShaguScore:round(ab / count, 2);

  if score ~= 0 then return score, ar, ag, ab, count else return nil end
end

function ShaguScore:GetItemLinkByName(name)
  for itemID = 1, 85000 do

    local itemName, hyperLink, itemQuality = GetItemInfo(itemID)
    if (itemName and itemName == name) then
      local _, _, _, hex = GetItemQualityColor(tonumber(itemQuality))
      return hex.. "|H"..hyperLink.."|h["..itemName.."]|h|r"
    end
  end
end



function ShaguScore:GetRealItemID(name)
  for itemID = 1, 85000 do

    local itemName, hyperLink, itemQuality = GetItemInfo(itemID)
    if (itemName and itemName == name) then
      return itemID
    end
  end
end

function ShaguScore:GetRealItemLevel(OriginID)
  local level = GetItemLevel(OriginID) or 0
  return level
end

-- hooks
local ShaguScoreHookSetBagItem = GameTooltip.SetBagItem
function GameTooltip.SetBagItem(self, container, slot)
  GameTooltip.itemLink = GetContainerItemLink(container, slot)
  _, GameTooltip.itemCount = GetContainerItemInfo(container, slot)
  return ShaguScoreHookSetBagItem(self, container, slot)
end

local ShaguScoreHookSetQuestLogItem = GameTooltip.SetQuestLogItem
function GameTooltip.SetQuestLogItem(self, itemType, index)
  GameTooltip.itemLink = GetQuestLogItemLink(itemType, index)
  if not GameTooltip.itemLink then return end
  return ShaguScoreHookSetQuestLogItem(self, itemType, index)
end

local ShaguScoreHookSetQuestItem = GameTooltip.SetQuestItem
function GameTooltip.SetQuestItem(self, itemType, index)
  GameTooltip.itemLink = GetQuestItemLink(itemType, index)
  return ShaguScoreHookSetQuestItem(self, itemType, index)
end

local ShaguScoreHookSetLootItem = GameTooltip.SetLootItem
function GameTooltip.SetLootItem(self, slot)
  GameTooltip.itemLink = GetLootSlotLink(slot)
  ShaguScoreHookSetLootItem(self, slot)
end

local ShaguScoreHookSetInboxItem = GameTooltip.SetInboxItem
function GameTooltip.SetInboxItem(self, mailID, attachmentIndex)
  local itemName, itemTexture, inboxItemCount, inboxItemQuality = GetInboxItem(mailID)
  GameTooltip.itemLink = ShaguScore:GetItemLinkByName(itemName)
  return ShaguScoreHookSetInboxItem(self, mailID, attachmentIndex)
end

local ShaguScoreHookSetInventoryItem = GameTooltip.SetInventoryItem
function GameTooltip.SetInventoryItem(self, unit, slot)
  GameTooltip.itemLink = GetInventoryItemLink(unit, slot)
  return ShaguScoreHookSetInventoryItem(self, unit, slot)
end

local ShaguScoreHookSetLootRollItem = GameTooltip.SetLootRollItem
function GameTooltip.SetLootRollItem(self, id)
  GameTooltip.itemLink = GetLootRollItemLink(id)
  return ShaguScoreHookSetLootRollItem(self, id)
end

local ShaguScoreHookSetLootRollItem = GameTooltip.SetLootRollItem
function GameTooltip.SetLootRollItem(self, id)
  GameTooltip.itemLink = GetLootRollItemLink(id)
  return ShaguScoreHookSetLootRollItem(self, id)
end

local ShaguScoreHookSetMerchantItem = GameTooltip.SetMerchantItem
function GameTooltip.SetMerchantItem(self, merchantIndex)
  GameTooltip.itemLink = GetMerchantItemLink(merchantIndex)
  return ShaguScoreHookSetMerchantItem(self, merchantIndex)
end

local ShaguScoreHookSetCraftItem = GameTooltip.SetCraftItem
function GameTooltip.SetCraftItem(self, skill, slot)
  GameTooltip.itemLink = GetCraftReagentItemLink(skill, slot)
  return ShaguScoreHookSetCraftItem(self, skill, slot)
end

local ShaguScoreHookSetCraftSpell = GameTooltip.SetCraftSpell
function GameTooltip.SetCraftSpell(self, slot)
  GameTooltip.itemLink = GetCraftItemLink(slot)
  return ShaguScoreHookSetCraftSpell(self, slot)
end

local ShaguScoreHookSetTradeSkillItem = GameTooltip.SetTradeSkillItem
function GameTooltip.SetTradeSkillItem(self, skillIndex, reagentIndex)
  if reagentIndex then
    GameTooltip.itemLink = GetTradeSkillReagentItemLink(skillIndex, reagentIndex)
  else
    GameTooltip.itemLink = GetTradeSkillItemLink(skillIndex)
  end
  return ShaguScoreHookSetTradeSkillItem(self, skillIndex, reagentIndex)
end

local HookSetAuctionItem = GameTooltip.SetAuctionItem
function GameTooltip.SetAuctionItem(self, atype, index)
  local itemName, _, itemCount = GetAuctionItemInfo(atype, index)
  GameTooltip.itemCount = itemCount
  GameTooltip.itemLink = GetItemLinkByName(itemName)
  return HookSetAuctionItem(self, atype, index)
end

local ShaguScoreHookSetAuctionSellItem = GameTooltip.SetAuctionSellItem
function GameTooltip.SetAuctionSellItem(self)
  local itemName, _, itemCount = GetAuctionSellItemInfo()
  GameTooltip.itemCount = itemCount
  GameTooltip.itemLink = ShaguScore:GetItemLinkByName(itemName)
  return ShaguScoreHookSetAuctionSellItem(self)
end

local ShaguScoreHookSetTradePlayerItem = GameTooltip.SetTradePlayerItem
function GameTooltip.SetTradePlayerItem(self, index)
  GameTooltip.itemLink = GetTradePlayerItemLink(index)
  return ShaguScoreHookSetTradePlayerItem(self, index)
end

local ShaguScoreHookSetTradeTargetItem = GameTooltip.SetTradeTargetItem
function GameTooltip.SetTradeTargetItem(self, index)
  GameTooltip.itemLink = GetTradeTargetItemLink(index)
  return ShaguScoreHookSetTradeTargetItem(self, index)
end

-- database
ShaguScore.Database = {}

ShaguScore:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
ShaguScore:SetScript("OnEvent", function()
  score, r, g, b = ShaguScore:ScanUnit("mouseover")
  if score and r and g and b then
    GameTooltip:AddLine("Emberscore: " .. score, r,g,b)
    GameTooltip:Show()
  end
end)

-- slash command handler
SLASH_EMBERSCORE1 = "/emberscore";
local function slashCommand()
  local userName = UnitName("target")
  local score, r, g, b , count = ShaguScore:ScanUnit("target")
  if score and r and g and b then
    local hex = rgbToHex(r*255,g*255,b*255)
    local startLine = "\124"
    local endLine = "\124r"
    printLine(tostring(UnitClass("target")) .. ": " .. tostring(userName) .. ": " .. tostring(score));
  end
end
SlashCmdList["EMBERSCORE"] = slashCommand;